package com.lyu.travel.controller;

import com.lyu.travel.core.entity.Place;
import com.lyu.travel.core.entity.Province;
import com.lyu.travel.core.service.PlaceService;
import com.lyu.travel.core.service.ProvinceService;
import com.lyu.travel.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: [travel]
 * @Package: [com.lyu.travel.controller]
 * @ClassName: [PlaceController]
 * @Description: 景点控制类
 * @Author: [78483]
 * @CreateDate: [2020-06-14 10:09]
 * @UpdateUser: [78483]
 * @UpdateDate: [2020-06-14 10:09]
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 */
@Controller
public class PlaceController {
    @Autowired
    PlaceService placeService;

    @Autowired
    ProvinceService provinceService;


    //景点管理
    @GetMapping("/place")
    public String place(Model model) {
        //业务处理逻辑
        model.addAttribute("placeList", placeService.findAll());
        model.addAttribute("placeList1", provinceService.findAll());
        return "placeList";
    }

    //添加
    @PostMapping("/plaSave")
    @ResponseBody
    public  Object savePla(Place place){
        //处理请求
        Map map= new HashMap<>();
        if (placeService.findPlaceByname(place.getName())!=null){
            map.put("msg1", "景点名重复");
        }else if (place.getName()=="") {

            map.put("msg2","景点名不能为空");
        }else{
            placeService.insert(place);
            map.put("success",true);
            map.put("data",place);
        }
        return map  ;
    }

    //删除
    @PostMapping("/plaDelete")
    @ResponseBody
    public int deletePla(int id){
        //处理请求
        return placeService.deletePlaceById(id);
    }


    //更新
    @PostMapping("/plaUpdate")
    @ResponseBody
    public Object updatePla(Place place){
        //处理请求
        Map map= new HashMap<>();
        if (place.getName()=="") {
            map.put("msg2", "不能为空");
        }else{
            placeService.updatePlace(place);
        }
        return map  ;
    }


}



